package com.pearadmin.edu.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.edu.domain.Course;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.edu.service.ICourseService;

import java.time.LocalDateTime;

/**
 * 课程管理Controller
 *
 * @author yanyi
 * @date 2021-06-09
 */
@RestController
@RequestMapping("/edu/course")
public class CourseController extends BaseController
{
    private String prefix = "edu/course";

    @Autowired
    private ICourseService courseService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/edu/course/main','edu:course:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询课程管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/edu/course/data','edu:course:data')")
    public ResultTable list(@ModelAttribute Course course, PageDomain pageDomain)
    {
        PageInfo<Course> pageInfo = courseService.selectCoursePage(course,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增课程管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/edu/course/add','edu:course:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存课程管理
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/edu/course/add','edu:course:add')")
    public Result save(@RequestBody Course course)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        course.setCreateTime(LocalDateTime.now());
        course.setCreateBy(sysUser.getUserId());
        course.setCreateName(sysUser.getUsername());
        return decide(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/edu/course/edit','edu:course:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存课程管理
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/edu/course/edit','edu:course:edit')")
    public Result update(@RequestBody Course course)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        course.setUpdateTime(LocalDateTime.now());
        course.setUpdateBy(sysUser.getUserId());
        course.setUpdateName(sysUser.getUsername());
        return decide(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/edu/course/remove','edu:course:remove')")
    public Result batchRemove(String ids)
    {
        return decide(courseService.deleteCourseByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/edu/course/remove','edu:course:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(courseService.deleteCourseById(id));
    }
}
